package com.jdiv.samples.truco;

import java.util.Random;

public class Mazo {
	
	private final int MAZO_MAX 	 = 40;
	private 	  int mazo[] 	 = new int[MAZO_MAX]; 
	private 	  int posCarta	 = 0;
    
    public Mazo(){
    	
    }
    
	public void iniciarMazo() {
		
		int i;
		posCarta=0;
		
		for (i = 0; i < MAZO_MAX; i++) {
			mazo[i] = i;
		}
	}

	public void barajarMazo() {
		
		Random rnd = new Random(System.currentTimeMillis());
		int i, tmp;
		int indice, desp;
		posCarta = 0;
		
		for (i = 0; i < MAZO_MAX; i++) {
			desp = i;
			//numero aleatorio de la posicion a sustituir
			indice = (int) (rnd.nextInt() / (float) Integer.MAX_VALUE * (MAZO_MAX - i))
					+ desp; 
			//siempre positivo
			indice = Math.abs(indice);
			
			// Intercambio valores 
			tmp = mazo[i];
			mazo[i] = mazo[indice];
			mazo[indice] = tmp;
		}
		return;
	}

	public void mostrarMazo() {
		int i;
		for (i = 0; i < MAZO_MAX; i++) {
			if ((i % 5) == 0)
				System.out.println();
			
			System.out.print("[" + mazo[i] + "]");
		}
		System.out.println();
		return;
	}
	
	public int getCarta(){
				
		if (posCarta < MAZO_MAX ){
			posCarta++; 
			return mazo[posCarta-1];
		} 
	 
		return -1;
	}
}
